package gameEngine.experiments;

import gameEngine.game.Game;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
/**
 * multi threaded Experiment, uses a thread pool to execute games simultaneously
 * @author alon
 *
 */
public class MTSimpleExperimentImpl implements Experiment {

	private List<Game> games;
	static ExecutorService execSvc;
	Logger log=Logger.getLogger(this.getClass());
	
	public void run() {
		log.debug("Experiment started");
		execSvc = Executors.newFixedThreadPool(4);
        for(int i=0;i<games.size();i++){
        	   log.debug("started game "+i);
               execSvc.execute(new GameExecuter(games.get(i)));           
         }
        execSvc.shutdown();
        try {
               execSvc.awaitTermination(10000, TimeUnit.MILLISECONDS);
         }catch (InterruptedException e) {
               log.debug(e.getStackTrace());
         }
	}
	
	public void addGame(Game game){
		games.add(game);
	}

}
